﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReporterEngine;
using ReporterControlLibrary.Models;

namespace ReporterControlLibrary
{
    /// <summary>
    /// Interaction logic for TemplateTree.xaml
    /// </summary>
    public partial class TemplateTree : UserControl
    {
        public Brush TreeBackground
        {
            get { return (Brush)this.GetValue(TreeBackgroundProperty); }
            set { this.SetValue(TreeBackgroundProperty, value); }
        }
        public static readonly DependencyProperty TreeBackgroundProperty = DependencyProperty.Register(
          "TreeBackground", typeof(Brush), typeof(TemplateTree), new PropertyMetadata(Brushes.Transparent));

        public TemplateTree()
        {
            InitializeComponent();
        }

        private void ModelTreeSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            //TODO: populate "((ReportInfo) this.DataContext).TemplateTreeInstancesSource" based on selection in this.modelTree

            var templateNode = e.NewValue as TemplateNode;

            if (templateNode == null)
                return;

            var strQuery = templateNode.Command.Query;

            var parent = templateNode.Parent;

            while(parent != null)
            {
                strQuery = parent.Command.Query + strQuery;
                parent = parent.Parent;
            }
            
            var ri = DataContext as ReportInfo;

            if (ri == null) return;
            // TODO: replace dummy implementationcode with seamless one ...
            var cifFile = ReporterSystem.ReportApp.GetCifPath(ri.Project.ReportProject);

            if (!System.IO.File.Exists(cifFile))
            {
                ReporterControlLibrary.MessageBox.Show(null, "Cif Model does not exists. Import notebook ... please", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            //select instances by actual path

            var actualPath = LexPath.Scan(strQuery);
            Interpreter target = Interpreter.Create(actualPath);

            var model = ReporterSystem.ReportApp.GetModel(ri.Project.ReportProject);

            sQuery query = sQuery.New(model);
            query.Elements.Add(model.Notebook);

            var actual = target.Execute(actualPath, query);

            ri.TemplateTreeInstancesSource = model.GetInstanceCategories(actual);


            //if (ri.TemplateTreeInstancesSource == null)
            //{
            //    System.Threading.Tasks.Task.Factory.StartNew(() =>
            //    {


            //        ri.TemplateTreeInstancesSource = model.GetInstanceCategories();
            //    });
            //}
        }
    }
}
